/**
 * 系统设置控制器
 */
import $_fn from '~plugins'
import './index.less'
export default ['$scope','$rootScope','$location','$ui','http','$state',function($scope,$rootScope,$location,$ui,http,$state) {
    // $scope.tabMenu = $_fn.getCurrMenu($location.path())
    // console.log($location.path(), $scope.tabMenu)

  $scope.redErr = {}
  //url参数获取
  $scope.search = $location.search()
  //页面操作项分类
  $scope.state = $scope.search.state || 1
  //警告提示
  $scope.errorToast = function(text) {
    $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
  }
  //成功提示
  $scope.successToast = function(text) {
      $ui.toast({text:text,type:'success',position:'center',appendTo:'.wp-container'});
  }
  $scope.remHight = function(){                                                 //去除报错高亮
    for(var i in $scope.redErr){
      $scope.redErr[i] = false
    }
  }


  //获取数据
  $scope.init = function(page,params){
    if($scope.state==1){
      http.post('selectGInterfaceService.do',{id:1},function (data) {
      if(data.errcode == 1){
        $scope.$apply(function () {
          $scope.data = data.data;
            
          });
        }else{
          $scope.errorToast(data.errmsg || '请求失败！')
        }
      })
    }else{
      http.post('selectEmailService.do',{},function (data) {
      if(data.errcode == 1){
        $scope.$apply(function () {
          $scope.param = data;
            $(".slide-btn span").siblings("span").removeClass("D-queding");
            if($scope.param.is_open==1){
              $(".slide-btn span").eq(0).addClass("D-queding");
            }else {
              $(".slide-btn span").eq(1).addClass("D-queding");
            }
          });
        }else{
          $scope.errorToast(data.errmsg || '请求失败！')
        }
      })
    }

  }
  //初始化数据
  $scope.init()

  $scope.openSet = function(e){
    console.log('12334',e.target)
    $(e.target).addClass("D-queding");
    $(e.target).siblings("span").removeClass("D-queding");
  }

  $scope.ceshiMail=function(){
    http.post("sendEmailTest.do", $scope.param, function (data) {
      console.log(data);
      if(data.type>0){
        $scope.successToast(data.errmsg)
      }else{
        $scope.errorToast(data.errmsg || '请求失败！')
      };
    });
  }

  $scope.queding=function(){
    var panduanZ=$(".D-queding").text();
    var panduan;
    if(panduanZ=="是"){
      panduan=1;
    }else{
      panduan=2;
    }
    $scope.param.is_open=panduan;
    $scope.param.serverSMTP=$scope.param.server;
    $scope.param.portSMTP=$scope.param.port;
    $scope.param.send_mail=$scope.param.send_mail;
    $scope.param.e_nameSMTP=$scope.param.e_name;
    $scope.param.e_pwdSMTP=$scope.param.e_pwd;
    $scope.param.test_mail=$scope.param.test_mail;
    console.log($scope.param);
    http.post("updateEmailService.do", $scope.param, function (data) {
      console.log(data);
      if(data.errcode>0){
        $scope.successToast(data.errmsg)
      }else{
        $scope.errorToast(data.errmsg || '请求失败！')
      };
    });
  }
  //
  $scope.ok = function () {

    /*if ($scope.data.username==null || $scope.data.username==undefined) {
      $scope.redErr.username = true
      $scope.errorToast('请填写用户名')
      return
    }*/

    http.post('updateGInterfaceService.do', $scope.data, function (data) {
      if(data.errcode == 1){
        $scope.$apply(function(){
          $scope.successToast(data.errmsg)
        })
      }else{
        $scope.errorToast(data.errmsg || '请求失败！')
      }
    })
  }
}]
